home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / prog_c / cuj0696.zip / SCHULIST.ZIP / MATRIX.H < prev   
C/C++ Source or Header  |  1996-04-08  |  6KB  |  244 lines

  1. // Stephen O. Schulist - 1 September 1995
  2.  
  3. #ifndef _SOS_MATRIX_H
  4. #define _SOS_MATRIX_H
  5.  
  6. #include "array.h"
  7.  
  8. template<class T> class Matrix
  9. {
  10. public:
  11.  
  12.    Matrix(int nRows = 0, int nCols = 0);
  13.    Matrix(const Matrix<T>& matrix);
  14.   ~Matrix();
  15.  
  16.   Matrix<T>& operator=(const Matrix<T>& matrix);
  17.  
  18.   int operator==(const Matrix<T>& matrix) const;
  19.   int operator!=(const Matrix<T>& matrix) const;
  20.  
  21.   int    Rows() const { return m_nRows; }
  22.   int Columns() const { return m_nCols; }
  23.  
  24.   Matrix<T> operator-() const;
  25.   Matrix<T> operator~() const;
  26.  
  27.   Matrix<T> operator+(const Matrix<T>& m) const;
  28.   Matrix<T> operator-(const Matrix<T>& m) const { return *this + -m; }
  29.   Matrix<T> operator*(const T& t) const;
  30.   Matrix<T> operator/(const T& t) const { return *this * (1/t); }
  31.  
  32.   Matrix<T>& operator+=(const Matrix<T>& m) { return *this = *this + m; }
  33.   Matrix<T>& operator-=(const Matrix<T>& m) { return *this = *this - m; }
  34.   Matrix<T>& operator*=(const T& t)         { return *this = *this * t; }
  35.   Matrix<T>& operator/=(const T& t)         { return *this = *this / t; }
  36.  
  37.   friend Matrix<T> operator*(const T& t, const Matrix<T>& m) { return m * t; }
  38.  
  39.   Matrix<T> operator*(const Matrix<T>& m) const;
  40.  
  41.   Matrix<T>    Row(int nRow) const;
  42.   Matrix<T> Column(int nCol) const;
  43.  
  44.   void SetRow   (int nRow, const Matrix<T>& matrix);
  45.   void SetColumn(int nCol, const Matrix<T>& matrix);
  46.  
  47.   const T& operator()(int nRow, int nCol) const;
  48.         T& operator()(int nRow, int nCol);
  49.  
  50. private:
  51.  
  52.   int m_nRows;
  53.   int m_nCols;
  54.  
  55.   Array<T> m_data;
  56. };
  57.  
  58. template<class T> inline Matrix<T>::Matrix(int nRows, int nCols) :
  59.   m_nRows(nRows), m_nCols(nCols), m_data(nRows*nCols)
  60. {
  61. }
  62.  
  63. template<class T> inline Matrix<T>::Matrix(const Matrix<T>& matrix) :
  64.   m_nRows(matrix.m_nRows), m_nCols(matrix.m_nCols), m_data(matrix.m_data)
  65. {
  66. }
  67.  
  68. template<class T> inline Matrix<T>::~Matrix()
  69. {
  70. }
  71.  
  72. template<class T> inline Matrix<T>& Matrix<T>::operator=(
  73.   const Matrix<T>& matrix)
  74. {
  75.   if ( this != &matrix )
  76.   {
  77.     m_nRows = matrix.m_nRows;
  78.     m_nCols = matrix.m_nCols;
  79.     m_data  = matrix.m_data;
  80.   }
  81.   return *this;
  82. }
  83.  
  84. template<class T> inline int Matrix<T>::operator==(
  85.   const Matrix<T>& matrix) const
  86. {
  87.   int equal = ( m_nRows == matrix.m_nRows ) && ( m_nCols == matrix.m_nCols );
  88.  
  89.   for ( int m = 0; equal && ( m < m_nRows ); m++ )
  90.   for ( int n = 0; equal && ( n < m_nCols ); n++ )
  91.     equal = ( (*this)(m,n) == matrix(m,n) );
  92.  
  93.   return equal;
  94. }
  95.  
  96. template<class T> inline int Matrix<T>::operator!=(
  97.   const Matrix<T>& matrix) const
  98. {
  99.   return !operator==(matrix);
  100. }
  101.  
  102. template<class T> inline Matrix<T> Matrix<T>::operator-() const
  103. {
  104.   Matrix<T> matrix(m_nRows, m_nCols);
  105.  
  106.   for ( int m = 0; m < m_nRows; m++ )
  107.   for ( int n = 0; n < m_nCols; n++ )
  108.     matrix(m, n) = -(*this)(m, n);
  109.  
  110.   return matrix;
  111. }
  112.  
  113. template<class T> inline Matrix<T> Matrix<T>::operator~() const
  114. {
  115.   Matrix<T> matrix(m_nCols, m_nRows);
  116.  
  117.   for ( int m = 0; m < m_nRows; m++ )
  118.   for ( int n = 0; n < m_nCols; n++ )
  119.     matrix(n, m) = (*this)(m, n);
  120.  
  121.   return matrix;
  122. }
  123.  
  124. template<class T> inline Matrix<T> Matrix<T>::operator+(const Matrix<T>& matrix) const
  125. {
  126.   ASSERT(m_nRows == matrix.m_nRows);
  127.   ASSERT(m_nCols == matrix.m_nCols);
  128.  
  129.   Matrix<T> tmp(m_nRows, m_nCols);
  130.  
  131.   for ( int m = 0; m < m_nRows; m++ )
  132.   for ( int n = 0; n < m_nCols; n++ )
  133.     tmp(m, n) = (*this)(m, n) + matrix(m, n);
  134.  
  135.   return tmp;
  136. }
  137.  
  138. template<class T> inline const T& Matrix<T>::operator()(
  139.   int nRow, int nCol) const
  140. {
  141.   ASSERT(nRow < m_nRows);
  142.   ASSERT(nCol < m_nCols);
  143.  
  144.   return m_data[nRow*m_nCols + nCol];
  145. }
  146.  
  147. template<class T> inline T& Matrix<T>::operator()(int nRow, int nCol)
  148. {
  149.   ASSERT(nRow < m_nRows);
  150.   ASSERT(nCol < m_nCols);
  151.  
  152.   return m_data[nRow*m_nCols + nCol];
  153. }
  154.  
  155. template<class T> inline Matrix<T> Matrix<T>::operator*(const T& t) const
  156. {
  157.   Matrix<T> matrix(m_nRows, m_nCols);
  158.  
  159.   for ( int m = 0; m < m_nRows; m++ )
  160.   for ( int n = 0; n < m_nCols; n++ )
  161.     matrix(m, n) = (*this)(m, n) * t;
  162.  
  163.   return matrix;
  164. }
  165.  
  166. template<class T> inline Matrix<T> Matrix<T>::operator*(const Matrix<T>& matrix) const
  167. {
  168.   ASSERT(m_nCols == matrix.m_nRows);
  169.  
  170.   Matrix tmp(m_nRows, matrix.m_nCols);
  171.  
  172.   for ( int m = 0; m <        m_nRows; m++ )
  173.   for ( int n = 0; n < matrix.m_nCols; n++ )
  174.   {
  175.     tmp(m, n) = 0;
  176.     for ( int i = 0; i < m_nCols; i++ )
  177.       tmp(m, n) += (*this)(m, i) * matrix(i, n);
  178.   }
  179.  
  180.   return tmp;
  181. }
  182.  
  183. template<class T> inline Matrix<T> Matrix<T>::Row(int nRow) const
  184. {
  185.   ASSERT(nRow < m_nRows);
  186.  
  187.   Matrix<T> matrix(1, m_nCols);
  188.   for ( int n = 0; n < m_nCols; n++ )
  189.     matrix(0, n) = (*this)(nRow, n);
  190.   return matrix;
  191. }
  192.  
  193. template<class T> inline Matrix<T> Matrix<T>::Column(int nCol) const
  194. {
  195.   ASSERT(nCol < m_nCols);
  196.  
  197.   Matrix<T> matrix(m_nRows, 1);
  198.   for ( int m = 0; m < m_nRows; m++ )
  199.     matrix(m, 0) = (*this)(m, nCol);
  200.   return matrix;
  201. }
  202.  
  203. template<class T> inline void Matrix<T>::SetRow(
  204.   int nRow, const Matrix<T>& matrix)
  205. {
  206.   ASSERT(nRow < m_nRows);
  207.  
  208.   ASSERT(matrix.Rows() == 1);
  209.   ASSERT(matrix.Columns() == m_nCols);
  210.  
  211.   for ( int n = 0; n < m_nCols; n++ )
  212.     (*this)(nRow, n) = matrix(0, n);
  213. }
  214.  
  215. template<class T> inline void Matrix<T>::SetColumn(
  216.   int nCol, const Matrix<T>& matrix)
  217. {
  218.   ASSERT(nCol < m_nCols);
  219.  
  220.   ASSERT(matrix.Rows() == m_nRows);
  221.   ASSERT(matrix.Columns() == 1);
  222.  
  223.   for ( int m = 0; m < m_nRows; m++ )
  224.     (*this)(m, nCol) = matrix(m, 0);
  225. }
  226.  
  227. template<class T> inline ostream& operator<<(
  228.   ostream& stream, const Matrix<T>& matrix)
  229. {
  230.   for ( int m = 0; m < matrix.Rows(); m++ )
  231.   {
  232.     for ( int n = 0; n < matrix.Columns(); n++ )
  233.     {
  234.       stream << matrix(m,n) << " ";
  235.     }
  236.     stream << endl;
  237.   }
  238.   return stream;
  239. }
  240.  
  241. #endif // MATRIX_H
  242.  
  243.  
  244.